package com.second.app.xml;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author GYZ
 * @Date 2024/2/27
 * <pre>
 *
 * </pre>
 */
public class Main {

    public static void main(String[] args) throws ParserConfigurationException, IOException, SAXException {
        String xmlPath = "E:\\second-app\\src\\main\\resources\\studentXml.xml";

        Document document = DocumentParser.getDocument(xmlPath);
        List<StudentXml> studentXmls = new ArrayList<>();

        NodeList studentNodes = document.getElementsByTagName("student");

        for (int i = 0; i < studentNodes.getLength(); i++) {
            Element studentEle = (Element) studentNodes.item(i);

            StudentXml studentXml = new StudentXml();
            studentXml.setName(studentEle.getElementsByTagName("name").item(0).getTextContent());
            studentXml.setAge(studentEle.getElementsByTagName("age").item(0).getTextContent());
            studentXmls.add(studentXml);
        }

        for (StudentXml studentXml : studentXmls) {
            System.out.println(studentXml);
        }

    }
}
